package com.efounder.builder.base.data;

import java.util.*;

import com.efounder.builder.base.util.*;
//import javax.swing.*;
import java.math.*;

import com.efounder.eai.data.KeyValue;

/**
 * @author Skyline
 */
public class EFRowSet
        implements ESPRowSet, java.io.Serializable, KeyValue {//extends HashMap
    /**
     *
     */
    protected transient java.util.Map frontID2NameMap = null;

    /**
     * @return Map
     */
    public java.util.Map getFrontID2NameMap() {
        return frontID2NameMap;
    }

    /**
     * @param fi2n Map
     */
    public void setFrontID2NameMap(java.util.Map fi2n) {
        frontID2NameMap = fi2n;
    }

    /**
     *
     */
    protected java.util.Map backID2NameMap = null;

    /**
     * @return Map
     */
    public java.util.Map getBackID2NameMap() {
        return backID2NameMap;
    }

    /**
     * @param bi2n Map
     */
    public void setBackID2NameMap(java.util.Map bi2n) {
        backID2NameMap = bi2n;
    }

    /**
     * @param ID      String
     * @param defName String
     * @return String
     */
    public Map CloneID2NameMap() {
        Map map = new HashMap();
        if (frontID2NameMap != null)
            map.putAll(frontID2NameMap);
        if (backID2NameMap != null)
            map.putAll(backID2NameMap);
        return map;
    }

    /**
     * @param map Map
     */
    public void setID2NameFront(Map map) {
        getID2Name("SB", "", "");
        if (frontID2NameMap == null) frontID2NameMap = new HashMap();
        frontID2NameMap.putAll(map);
    }

    /**
     * @param ColID   String
     * @param ID      String
     * @param defName String
     * @return String
     */
    public String getID2Name(String DCT_ID, String COL_ID, String defName) {
        if (frontID2NameMap == null) {
            frontID2NameMap = backID2NameMap;
            backID2NameMap = null;
        }
        Object res = null;
        if (frontID2NameMap != null) {
            res = frontID2NameMap.get(DCT_ID + "_" + COL_ID);
            if (res != null) return res.toString();
        }
        return defName;
    }

    /**
     * @param DCT_ID String
     * @param COL_ID String
     * @return ESPRowSet
     */
    public ESPRowSet getID2RowSet(String DCT_ID, String COL_ID) {
        if (frontID2NameMap == null) {
            frontID2NameMap = backID2NameMap;
            backID2NameMap = null;
        }
        Object res = null;
        if (frontID2NameMap != null) {
            res = frontID2NameMap.get(DCT_ID + "_RS_" + COL_ID);
        }
        return (ESPRowSet) res;
    }

    /**
     * @return String
     */
    public String toString() {
        return this.getString("caption", "");
    }

    /**
     * @param s String
     */
    public void setCaption(String s) {
        this.putString("caption", s);
    }

    /**
     * @param ID   String
     * @param Name String
     */
    public void setID2Name(String DCT_ID, String COL_ID, String Name) {
        // 如果frontID2NameMap不为空，说明是在前台，所有放入其中，直接返回
        if (frontID2NameMap != null) {
            frontID2NameMap.put(DCT_ID + "_" + COL_ID, Name);
            return;
        }
        // 执行到这，说明是在后台
        if (backID2NameMap == null) backID2NameMap = new java.util.HashMap();
        // 放入后台的Map中，可以序列化
        backID2NameMap.put(DCT_ID + "_" + COL_ID, Name);
    }

    /**
     * @param DCT_ID String
     * @param COL_ID String
     * @param rowSet ESPRowSet
     */
    public void setID2RowSet(String DCT_ID, String COL_ID, ESPRowSet rowSet) {
        // 如果frontID2NameMap不为空，说明是在前台，所有放入其中，直接返回
        if (frontID2NameMap != null) {
            frontID2NameMap.put(DCT_ID + "_RS_" + COL_ID, rowSet);
            return;
        }
        // 执行到这，说明是在后台
        if (backID2NameMap == null) backID2NameMap = new java.util.HashMap();
        // 放入后台的Map中，可以序列化
        backID2NameMap.put(DCT_ID + "_RS_" + COL_ID, rowSet);
    }

    /**
     * @param DCT_ID String
     * @param COL_ID String
     * @param rowSet ESPRowSet
     */
    public void setID2RowSetFront(String DCT_ID, String COL_ID, ESPRowSet rowSet) {
        if (frontID2NameMap == null) frontID2NameMap = new java.util.HashMap();
        if (backID2NameMap != null) frontID2NameMap.putAll(backID2NameMap);
        backID2NameMap = null;
        frontID2NameMap.put(DCT_ID + "_RS_" + COL_ID, rowSet);
    }

    //
    static final long serialVersionUID = 1L;
    // 无需序列化
    protected transient java.util.List dataSetViewList = null;
    // 无需序列化
    protected transient java.util.List dataSetMapList = null;
    // 需要序列化
    protected java.util.List dataSetContList = null;

    /**
     * @return List
     */
    public java.util.List getDataSetContList() {
        return dataSetContList;
    }

    /**
     * @param dscl List
     */
    public void setDataSetContList(java.util.List dscl) {
        dataSetContList = dscl;
    }

    /**
     * @param dataSet EFDataSet
     */
    public void insertDataSetManager(EFDataSet dataSet) {
        if (dataSet.getDataSetType() == EFDataSet._DATA_SET_CONT_) {
            this.insertDataSetCont(dataSet);
            return;
        }
        if (dataSet.getDataSetType() == EFDataSet._DATA_SET_MAP_) {
            this.insertDataSetMap(dataSet);
            return;
        }
        if (dataSet.getDataSetType() == EFDataSet._DATA_SET_VIEW_) {
            this.insertDataSetView(dataSet);
            return;
        }
    }

    /**
     * @param dataSet EFDataSet
     */
    public void removeDataSetManager(EFDataSet dataSet) {
        if (dataSet.getDataSetType() == EFDataSet._DATA_SET_CONT_) {
            this.removeDataSetCont(dataSet);
            return;
        }
        if (dataSet.getDataSetType() == EFDataSet._DATA_SET_MAP_) {
            this.removeDataSetMap(dataSet);
            return;
        }
        if (dataSet.getDataSetType() == EFDataSet._DATA_SET_VIEW_) {
            this.removeDataSetView(dataSet);
            return;
        }
    }

    /**
     * @param dataSet EFDataSet
     */
    protected void insertDataSetMap(EFDataSet dataSet) {
        if (dataSetMapList == null) dataSetMapList = new java.util.ArrayList();
        dataSetMapList.add(dataSet);
    }

    /**
     * @param dataSet EFDataSet
     */
    protected void removeDataSetMap(EFDataSet dataSet) {
        if (dataSetMapList == null) return;
        dataSetMapList.remove(dataSet);
    }

    /**
     * @param dataSet EFDataSet
     */
    protected void insertDataSetCont(EFDataSet dataSet) {
        if (dataSetContList == null) dataSetContList = new java.util.ArrayList();
        dataSetContList.add(dataSet);
    }

    /**
     * @param dataSet EFDataSet
     */
    protected void removeDataSetCont(EFDataSet dataSet) {
        if (dataSetContList == null) return;
        dataSetContList.remove(dataSet);
    }

    /**
     * @param dataSet EFDataSet
     */
    protected void insertDataSetView(EFDataSet dataSet) {
        if (dataSetViewList == null) dataSetViewList = new java.util.ArrayList();
        dataSetViewList.add(dataSet);
    }

    /**
     * @param dataSet EFDataSet
     */
    protected void removeDataSetView(EFDataSet dataSet) {
        if (dataSetViewList == null) return;
        dataSetViewList.remove(dataSet);
    }

    /**
     *
     */
    protected transient boolean rowSelected = false;

    /**
     * @return boolean
     */
    public boolean isRowSelected() {
        return rowSelected;
    }

    /**
     * @param rowSelected boolean
     */
    public void setRowSelected(boolean rowSelected) {
        boolean oldValue = this.rowSelected;
        this.rowSelected = rowSelected;
        fireDataSetEvent("_ROW_SELECT_", oldValue, rowSelected);
    }
    /**
     *
     */
//  protected transient Icon rowIcon = null;
//  /**
//   *
//   * @return Icon
//   */
//  public Icon getRowIcon() {
//    return rowIcon;
//  }
//  /**
//   *
//   * @param rowIcon Icon
//   */
//  public void setRowIcon(Icon rowIcon) {
//    Icon oldIcon  = this.rowIcon;
//    this.rowIcon = rowIcon;
//    fireDataSetEvent("_ROW_ICON_",oldIcon,rowIcon);
//  }

    /**
     *
     */
    public EFRowSet() {
        dataMap = new HashMap();
    }

    /**
     * @return
     */
    public static EFRowSet getInstance() {
        EFRowSet rowSet = new EFRowSet();
        return rowSet;
    }

    /**
     *
     */
    private Map dataMap = null;
    /**
     *
     */
//  private EFDataSet childDataSet = null;

    /**
     * @return the dataMap
     */
    public Map<String, Object> getDataMap() {
        return dataMap;
    }

    /**
     * @param dataMap the dataMap to set
     */
    public void setDataMap(Map dataMap) {
        this.dataMap = dataMap;
    }

    /**
     * @return the formDataSet
     */
    public EFDataSet getChildDataSet() {
        return this.getDataSet(DATAConstant._CHILD_DATA_SET_);
    }

    /**
     * @param formDataSet the formDataSet to set
     */
    public void setChildDataSet(EFDataSet childDataSet) {
        this.setDataSet(DATAConstant._CHILD_DATA_SET_, childDataSet);
    }

    /**
     * @param keyName  String
     * @param defValue Boolean
     * @return Boolean
     */
    public Boolean getBoolean(String keyName, Boolean defValue) {
        // TODO Auto-generated method stub
        return MapUtil.getBoolean(dataMap, keyName, defValue, this.getExtRowSet(ESPRowSet._dependDCT_ITEM_, null));
    }

    /**
     * @param keyName  String
     * @param defValue Date
     * @return Date
     */
    public Date getDate(String keyName, Date defValue) {
        // TODO Auto-generated method stub
        return MapUtil.getDate(dataMap, keyName, defValue, this.getExtRowSet(ESPRowSet._dependDCT_ITEM_, null));
    }

    /**
     * @param keyName  String
     * @param defValue Number
     * @return Number
     */
    public Number getNumber(String keyName, Number defValue) {
        // TODO Auto-generated method stub
        return MapUtil.getNumber(dataMap, keyName, defValue, this.getExtRowSet(ESPRowSet._dependDCT_ITEM_, null));
    }

    /**
     * @param keyName  String
     * @param defValue String
     * @return String
     */
    public String getString(String keyName, String defValue) {
        // TODO Auto-generated method stub
        return MapUtil.getString(dataMap, keyName, defValue, this.getExtRowSet(ESPRowSet._dependDCT_ITEM_, null));
    }

    /**
     * @param keyName  String
     * @param defValue String
     * @return String
     */
    public String getStringByTrim(String keyName, String defValue) {
        String value = getString(keyName, defValue);
        if (value != null && value.trim().length() == 0) return defValue;
        return value;
    }

    /**
     * @param keyName String
     * @param value   Boolean
     */
    public void putBoolean(String keyName, Boolean value) {
        // TODO Auto-generated method stub
        putObject(keyName, value);
    }

    /**
     * @param keyName String
     * @param value   Date
     */
    public void putDate(String keyName, Date value) {
        // TODO Auto-generated method stub
        putObject(keyName, value);
//    MapUtil.putDate(dataMap, keyName, value);
    }

    /**
     * @param keyName String
     * @param value   Number
     */
    public void putNumber(String keyName, Number value) {
        // TODO Auto-generated method stub
        putObject(keyName, value);
    }

    /**
     * @param keyName String
     * @param value   String
     */
    public void putString(String keyName, String value) {
        // TODO Auto-generated method stub
        putString(keyName, value, true);
    }

    /**
     * @param keyName  String
     * @param defValue Object
     * @return Object
     */
    public Object getObject(String keyName, Object defValue) {
        // TODO Auto-generated method stub
        return MapUtil.getObject(dataMap, keyName, defValue);
    }

    /**
     * @param keyName String
     * @param value   Object
     */
    public void putString(String keyName, String value, boolean btrim) {
        if (btrim)
            putObject(keyName, value);
        else {
            Object oldValue = MapUtil.getObject(dataMap, keyName, null);
            MapUtil.putObject(dataMap, keyName, value);
            // 调用事件处理器
            fireDataSetEvent(keyName, oldValue, value);
        }
    }

    public void putObject(String keyName, Object value) {
        // TODO Auto-generated method stub
        // 去掉字符串中的空格
        if (value != null && value instanceof String) {
            value = ((String) value).trim();
        }
        Object oldValue = MapUtil.getObject(dataMap, keyName, null);
        MapUtil.putObject(dataMap, keyName, value);
        // 调用事件处理器
        fireDataSetEvent(keyName, oldValue, value);
    }

    public Set keySet() {
        // TODO Auto-generated method stub
        return this.dataMap.keySet();
    }

    public Object put(Object arg0, Object arg1) {
        // TODO Auto-generated method stub
        return this.dataMap.put(arg0, arg1);
    }

    /**
     * @param keyName  String
     * @param newValue Object
     */
    protected void fireDataSetEvent(String keyName, Object oldValue, Object newValue) {
        // 如果对象相等，不再触发事件
        if (oldValue == newValue) return;
        if (oldValue != null && oldValue.equals(newValue)) return;
        // 如果不是新建状态，则将状态改为修改
        if (this.dataStatus != ESPRowSet._DATA_STATUS_INSERT_)
            this.dataStatus = ESPRowSet._DATA_STATUS_UPDATE_;
        java.util.List dataSetList = new java.util.ArrayList();
        if (dataSetViewList != null) dataSetList.addAll(dataSetViewList);
        if (dataSetContList != null) dataSetList.addAll(dataSetContList);
        if (dataSetMapList != null) dataSetList.addAll(dataSetMapList);
        if (dataSetList.size() == 0) return;
//    Object oldValue = MapUtil.getObject(dataMap,keyName,null);
        EFDataSet dataSet = null;
        for (int i = 0; i < dataSetList.size(); i++) {
            dataSet = (EFDataSet) dataSetList.get(i);
            DataSetEvent dse = new DataSetEvent(dataSet, DataSetEvent.UPDATE);
            dse.setEventRowSet(this);
            dse.setUpdateRowSet(this);
            dse.setUpdateFieldName(keyName);
            dse.setUpdateOldValue(oldValue);
            dse.setUpdateNewValue(newValue);
            dataSet.fireDataSetEvent(dse);
        }
    }

    /**
     *
     */
    public java.util.HashMap<String, EFDataSet> dataSetMap = null;

    public java.util.HashMap<String, EFDataSet> getDataSetMap() {
        return dataSetMap;
    }

    public void setDataSetMap(java.util.HashMap<String, EFDataSet> dsm) {
        dataSetMap = dsm;
    }

    /**
     * @param dsType String
     * @return EFDataSet
     */
    public EFDataSet getDataSet(String dsType) {
        if (dataSetMap == null) return null;
        return dataSetMap.get(dsType);
    }

    /**
     * @param dsType  String
     * @param dataSet EFDataSet
     */
    public void setDataSet(String dsType, EFDataSet dataSet) {
        if (dataSetMap == null) dataSetMap = new java.util.HashMap<String, EFDataSet>();
        dataSetMap.put(dsType, dataSet);
        dataSet.setMasterKey(this);
        dataSet.setParentRowSet(this); // 设置DataSet的ParentRowSet
        dataSet.insertChildRowSet(this);
    }

    /**
     * @param dsType String
     * @return EFDataSet
     */
    public EFDataSet removeDataSet(String dsType) {
        if (dataSetMap == null) return null;
        EFDataSet dataSet = dataSetMap.remove(dsType);
        if (dataSet != null) {
            dataSet.setParentRowSet(null);
            dataSet.removeChildRowSet(this);
        }
        return dataSet;
    }

    /**
     * @return String[]
     */
    public String[] getDSTypes() {
        if (dataSetMap == null) return null;
        String[] dsTypes = new String[dataSetMap.size()];
        dsTypes = dataSetMap.keySet().toArray(dsTypes);
        return dsTypes;
    }

    /**
     *
     */
    protected Object master = null;

    /**
     * @param o Object
     */
    public void setMasterKey(Object o) {
        if (master != o) {
            master = o;
        }
    }

    /**
     * @return Object
     */
    public Object getMasterKey() {
        return master;
    }

    /**
     * @param key String
     * @param def double
     * @return double
     */
    public int getInt(String key, int def) {
        Object o = this.getObject(key, null);
        if (o == null) return def;
        if (o instanceof String)
            return Integer.parseInt((String) o);
        if (o instanceof BigDecimal) {
            return ((BigDecimal) o).intValue();
        }
        return def;
    }

    /**
     * @param key String
     * @param def double
     * @return double
     */
    public double getDouble(String key, double def) {
        if (getNumber(key, null) != null)
            return getNumber(key, null).doubleValue();
        String cysl = getString(key, String.valueOf(def));
        if (cysl.equals(""))
            cysl = "0";
        try {
            double dd = Double.parseDouble(cysl);
            if (Double.isNaN(dd)) return def;
            return dd;
        } catch (Exception e) {
            return def;
        }
    }

    /**
     * @param key Object
     * @param def Object
     * @return Object
     */
    public Object getValue(Object key, Object def) {
        return getObject(key.toString(), def);
    }

    /**
     * @param key Object
     * @param val Object
     */
    public void setValue(Object key, Object val) {
        this.putObject(key.toString(), val);
    }

    /**
     * @return Map
     */
    public Map getAttriMap() {
        return dataMap;
    }

    /**
     * 支持双向链表方式的数据集
     * 链表的构造由外部系统以工具类的方式构造，ESPRowSet不负责链表的构造，只提供链表方式的结构
     */


    /**
     *
     */
    protected ESPRowSet priorRowSet = null;

    /**
     * @return ESPRowSet
     */
    public ESPRowSet getPrior() {
        return priorRowSet;
    }

    /**
     * @param rowSet ESPRowSet
     */
    public void setPrior(ESPRowSet rowSet) {
        priorRowSet = rowSet;
    }

    /**
     *
     */
    protected ESPRowSet nextRowSet = null;

    /**
     * @return ESPRowSet
     */
    public ESPRowSet getNext() {
        return nextRowSet;
    }

    /**
     * @param rowSet ESPRowSet
     */
    public void setNext(ESPRowSet rowSet) {
        nextRowSet = rowSet;
    }

    /**
     * @return ESPRowSet
     */
    public ESPRowSet getFirst() {
        return null;
    }

    /**
     * @return ESPRowSet
     */
    public ESPRowSet getLast() {
        return null;
    }

    /**
     *
     */
    protected int dataStatus = _DATA_STATUS_NORMAL_;

    /**
     * @return int
     */
    public int getDataStatus() {
        return dataStatus;
    }

    /**
     * @param dataStatus int
     */
    public void setDataStatus(int dataStatus) {
        this.dataStatus = dataStatus;
    }

    /**
     *
     */
    public void setNewRowSetFlag() {
        this.dataStatus = _DATA_STATUS_INSERT_;
    }


    /**
     * 扩展属性存储，这些扩展属性不于数据列一起处理
     */
    protected java.util.Map extPropertys = null;

    /**
     * @return Map
     */
    public java.util.Map getExtPropertys() {
        return extPropertys;
    }

    /**
     * @param eps Map
     */
    public void setExtPropertys(java.util.Map eps) {
        extPropertys = eps;
    }

    /**
     * @param key   Object
     * @param value Object
     */
    public void setExtProperty(Object key, Object value) {
        if (extPropertys == null) extPropertys = new java.util.HashMap();
        extPropertys.put(key, value);
    }

    /**
     * @param key Object
     * @param def Object
     * @return Object
     */
    public Object getExtProperty(Object key, Object def) {
        if (extPropertys == null) return def;
        Object res = extPropertys.get(key);
        return res == null ? def : res;
    }

    /**
     *
     */
    protected int rowSetAppType = -1;

    /**
     * @return int
     */
    public int getRowSetAppType() {
        return rowSetAppType;
    }

    /**
     * @param rowSetType int
     */
    public void setRowSetAppType(int rowSetType) {
        rowSetAppType = rowSetType;
    }


    /**
     * @param key String
     * @return boolean
     */
    public boolean hasKey(String key) {
        if (dataMap == null) return false;
        Object[] keyObject = dataMap.keySet().toArray();
        String[] keyString = new String[keyObject.length];
        System.arraycopy(keyObject, 0, keyString, 0, keyString.length);
        return Arrays.asList(keyString).contains(key);
    }

    /**
     *
     */
    protected java.util.Map<String, ESPRowSet> extRowSetMap = null;

    /**
     * @param key    String
     * @param rowSet ESPRowSet
     */
    public void setExtRowSet(String key, ESPRowSet rowSet) {
        if (extRowSetMap == null) {
            extRowSetMap = new java.util.HashMap<String, ESPRowSet>();
        }
        extRowSetMap.put(key, rowSet);
    }

    /**
     * @param key String
     * @param def ESPRowSet
     * @return ESPRowSet
     */
    public ESPRowSet getExtRowSet(String key, ESPRowSet def) {
        if (extRowSetMap == null) return null;
        ESPRowSet rs = extRowSetMap.get(key);
        return (rs == null) ? def : rs;
    }

    public ESPRowSet getGroupDCT1RowSet() {
        return getExtRowSet(ESPRowSet._dependMDCT_ITEM_, null);
    }

    public ESPRowSet getGroupDCT2RowSet() {
        return getExtRowSet(ESPRowSet._dependDCT_ITEM_, null);
    }

    /**
     *
     */
    protected java.util.Map<String, String> rowColFormulaMap = null;

    /**
     * @param col String
     * @param def String
     * @return String
     */
    public String getRowColFormula(String col) {
        if (rowColFormulaMap == null) return rowFormula;
        return MapUtil.getString(rowColFormulaMap, col, rowFormula);
    }

    /**
     * @param col String
     * @param f1  String
     * @return String
     */
    public void setRowColFormula(String col, String f1) {
        if (rowColFormulaMap == null) rowColFormulaMap = new HashMap<String, String>();
        rowColFormulaMap.put(col, f1);
    }

    /**
     *
     */
    protected String rowFormula = null;

    /**
     * @param f1 String
     * @return String
     */
    public void setRowFormula(String f1) {
        rowFormula = f1;
    }

    /**
     *
     */
    protected int level;

    /**
     * @return int
     */
    public int getLevel() {
        return level;
    }

    /**
     * @param level int
     */
    public void setLevel(int level) {
        this.level = level;
    }

      public EFDataSet ownerDataSet = null;
    public EFDataSet getDataSet() {
        return ownerDataSet;
    }

    public void setDataSet(EFDataSet ds) {
    ownerDataSet = ds;
    }

    /**
     * @param isTrim 存储前是否进行trim处理
     */
    public void putObject(String keyName, Object value, boolean isTrim) {
        // TODO Auto-generated method stub
        // 去掉字符串中的空格
        if (value != null && value instanceof String && isTrim) {
            value = ((String) value).trim();
        }
        Object oldValue = MapUtil.getObject(dataMap, keyName, null);
        MapUtil.putObject(dataMap, keyName, value);
        // 调用事件处理器
        fireDataSetEvent(keyName, oldValue, value);
    }
}
